﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs;
using static Butcher.winform.EnterpriseForms.Eeductiones.DeductionsBackfill;

namespace Butcher.winform.EnterpriseForms.Eeductiones
{
    public partial class DeductionsBackfill : Form
    {
        public class deductions
        {
            public int pageinedex { get; set; } = 1;

            public int pagesize { get; set; } = 50;
            public string? DeductionName { get; set; } = null;
            public int? DeductionType { get; set; } = 0;
        };
        int cou = 0;
        public DeductionsBackfill()
        {
            InitializeComponent();
            uiDataGridView1.CellValueChanged += UiDataGridView1_CellValueChanged;

            int counts = 0;
            Deductions deductions = new Deductions();
            deductions deduc = new deductions();
            string url = @"http://localhost:5229/api/Section/liveDedu";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<deductions>(deduc), Encoding.UTF8);
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject josnresult = JObject.Parse(result);
            var results = josnresult["date"].Value<JArray>();
            var count = josnresult["totalCount"];
            counts = Convert.ToInt32(count);
            List<DeductionDTO> ssss = results.ToObject<List<DeductionDTO>>();
            int columnIndex = 1;
            string[] columnData = new string[uiDataGridView1.Rows.Count];
            cou = ssss.Count;
            uiDataGridView1.DataSource = ssss;
        }
        int indexs = 0;
        public void UiDataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            // 在这里可以编写处理单元格值变化的逻辑
            // sender 表示触发事件的DataGridView控件本身
            // e 参数包含了一些关于发生变化的单元格的信息，比如行索引、列索引等
            DataGridView gridView = (DataGridView)sender;
            int rowIndex = e.RowIndex;
            int columnIndex = e.ColumnIndex;
            // 例如，可以获取变化后单元格的值
            if (Convert.ToInt32(uiDataGridView1.Rows[0].Cells[1].Value) > 0)
            {
                return;
            }
            object newValue = gridView.Rows[rowIndex].Cells[columnIndex].Value;
            object sum = uiDataGridView1.Rows[rowIndex].Cells[columnIndex + 3].Value;
            uiDataGridView1.Rows[rowIndex].Cells[columnIndex + 1].Value = Convert.ToDecimal(gridView.Rows[rowIndex].Cells[columnIndex].Value) * Convert.ToDecimal(uiDataGridView1.Rows[rowIndex].Cells[columnIndex + 3].Value);
            indexs++;
        }



        private void uiFlowLayoutPanel3_Load(object sender, EventArgs e)
        {
            int count = 13;
            Button[] buttons = new Button[count];
            for (int i = 1; i < count; i++)
            {
                buttons[i] = new Button();
                buttons[i].Location = new Point(i * 150 + 150, 100);
                buttons[i].Size = new Size(100, 80);
                buttons[i].Text = i.ToString();
                buttons[i].Name = i.ToString();
                if (i == 10)
                {
                    buttons[i].Text = "0";
                    buttons[i].Name = "0";
                }
                if (i == 11)
                {
                    buttons[i].Text = "清零";
                    buttons[i].Name = "清零";
                }
                if (i == 12)
                {
                    buttons[i].Text = "退格";
                    buttons[i].Name = "退格";
                }
                buttons[i].Click += new System.EventHandler(this.button_Click);
                uiFlowLayoutPanel3.Controls.Add(buttons[i]);
            }
        }
        private void button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            string[] str = new string[uiDataGridView1.Rows.Count];
            for (int i = 0; i < uiDataGridView1.Rows.Count; i++)
            {
                if (uiDataGridView1.Rows[i].Selected == true)
                {
                    str[i] = uiDataGridView1.Rows[i].Cells[1].Value.ToString();
                }
            }
            MessageBox.Show(button.Name);
        }



        private void uiPanel1_Load(object sender, EventArgs e)
        {


        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        public static string Deuvalue = "";
        private void uiButton2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < cou; i++)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(uiDataGridView1.Rows[i].Cells[1].Value)))
                {
                    Deuvalue += Convert.ToString(uiDataGridView1.Rows[i].Cells[2].Value) + " " + Convert.ToString(uiDataGridView1.Rows[i].Cells[0].Value)+",";
                }
            }
            //if (!string.IsNullOrEmpty(Deuvalue))
            //{
            //    Deuvalue.TrimEnd();
            //}
        }
    }
}
